using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyTaskWander : Node
{
    private EnemyControl enemyControl;
    private int moveDir = 1;
    private int dir = 1;

    
    // 构造函数，创建对象时，传入所需要的参数
    public EnemyTaskWander(EnemyControl enemyControl)
    {
        this.enemyControl = enemyControl;
    }
    
    //
    public override NodeState Evaluate()
    {
        // 巡逻功能实现
        enemyControl.GetAnimator.Play("SlimeGRun");
        SetVelocityX(enemyControl.GetEnemyMoveSpeed, moveDir);
        enemyControl.SetLocalScale(dir);
        // 到达左边界，向右
        if (Vector2.Distance(enemyControl.GetLeft.position, enemyControl.gameObject.transform.position) < 0.1f)
        {
            moveDir = dir = 1;
        }
        if (Vector2.Distance(enemyControl.GetRight.position, enemyControl.gameObject.transform.position) < 0.1f)
        {
            moveDir = dir = -1;
        }
        
        // 返回状态，running，执行完继续执行
        state = NodeState.RUNNING;
        return state;
    }
    
    /// <summary>
    /// 设置X轴速度
    /// </summary>
    /// <param name="velocityX">速度</param>
    /// <param name="moveDir">朝向</param>
    public void SetVelocityX(float velocityX, float moveDir)
    {
        enemyControl.GetRb.velocity = new Vector2(velocityX * moveDir, enemyControl.GetRb.velocity.y);
    }
}
